$(function () {
    initTree();
    initTableData(1);

    $("#table-info").bind('click', searchUser);
});

//user域的全局变量机构id
var unit_Id = "";

function initTree() {
    var ztree;
    var setting = {
        async: {
            enable: true,
            url: base+"/private/sys/role/usertree",
            autoParam: ["id"]
        }
        ,view: {
            nameIsHTML: true
        }
        ,data: {
            simpleData: {
                enable: true
            }
        }
    };
    return ztree = $.fn.zTree.init($("#treeDemo"), setting);
}

var initTableData = function(num){
    var param = {
        unitId:unit_Id,
        page_num:num,
        page_size:"10"
    }
    $.ajax({
        url: base + "/private/sys/user/listUserByUnitId",
        data: param,
        type:"post",
        success: function (data, textStatus, jqXHR) {
            data=JSON.parse(data);
            var pageCount = data.pageCount;
            var dataObj ={
                "list":data.rows,
                "dataTool":{
                    "title":{
                        "loginname": "用户名"
                        ,"realname": "姓名"
                        ,"state":"帐户状态"
                        ,"position":"职务"
                        ,"email":"电子邮件"
                        ,"telephone":"固定电话"
                        ,"mobile":"手机号码"
                    },
                    "titleCheckbox":true,
                    "noDataStatus":true
                }
            };

            showPageTable('userlist', dataObj,num,pageCount,"initTableData");
        },
        error: function () {
        }
    });
}


var view=function(unitId){
    //此处初始化机构id方便表格查询显示
    unit_Id=unitId;
    initTableData(1);
}

function searchUserName() {
    if(unit_Id==null || unit_Id=="" || unit_Id==undefined){
        layer.msg("请选择机构！", {
            time: 1000, //1s后自动关闭
        });
        return;
    }
    searchUser(unit_Id,1);
}

var  searchUser=function (unit_Id,num) {
    var param = {
        unitId:unit_Id,
        page_num:num,
        page_size:"10",
        SearchUserName:$("input[name='user_name']").val()
    }
    $.ajax({
        url: base + "/private/sys/user/listUserByUnitId",
        data: param,
        type:"post",
        success: function (data, textStatus, jqXHR) {
            data=JSON.parse(data);
            var pageCount = data.pageCount;
            var dataObj ={
                "list":data.rows,
                "dataTool":{
                    "title":{
                        "loginname": "用户名"
                        ,"realname": "姓名"
                        ,"state":"帐户状态"
                        ,"position":"职务"
                        ,"email":"电子邮件"
                        ,"telephone":"固定电话"
                        ,"mobile":"手机号码"
                    },
                    "titleCheckbox":true,
                    "noDataStatus":true
                }
            };

            showPageTable('userlist', dataObj,num,pageCount,"searchUser");
        },
        error: function () {
        }
    });
}
